/**
 * 全局hooks
 */
 import type { FollowType } from '@/types/consult'
 import { attentionDoctorAPI } from '@/api/consult'
import { Toast } from 'vant'
import { getPrescriptionPic } from '@/api/consult'
import { ImagePreview } from 'vant'
import { getMedicalOrderDetail } from '@/api/medicine'
import type { OrderDetail } from '@/types/medicine'
import { onMounted, ref } from 'vue'
export const useFollow = (type:FollowType='doc') => {
  const loading = ref(false)
  const changeFlag = async (doc: {
    id: string
    likeFlag: 0 | 1
  }) => {
  loading.value = true
  try {
    await attentionDoctorAPI(type, doc.id)
    doc.likeFlag = doc.likeFlag === 1 ? 0 : 1
    console.log(doc)
    Toast.success(doc.likeFlag === 1 ? '关注成功' : '取消关注')
  } finally {
    loading.value = false
  }
  }
  return {loading,changeFlag}
}


// 查看处方
const useLookpre = () => {
  const showPrescription = async (id?: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      // 实现图片的预览
      ImagePreview([res.data.url])
    }
  
  }
  return {showPrescription}
}
// 
const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
      console.log('药品订单详情',res.data)
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}


export {useLookpre,useOrderDetail}